﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IHISCETeim.Library
{
    /// <summary>
    /// 提供对List集合对象扩展常用的方法
    /// </summary>
    public static class ListExt
    {
        /// <summary>
        /// 将一个集合中的元素附加到源集合结尾处（要附加的元素若在源集合存在，则不附加，否则附加）
        /// </summary>
        /// <typeparam name="T">集合中元素的数据类型</typeparam>
        /// <param name="sourceList">源集合</param>
        /// <param name="list">要附加的集合</param>
        public static void AddList<T>(this List<T> sourceList, List<T> list)
        {
            if (list != null && list.Count > 0)
            {
                foreach (T item in list)
                {
                    if (!sourceList.Contains(item))
                    {
                        sourceList.Add(item);
                    }
                }
            }
        }

        /// <summary>
        /// 获取源集合的交集
        /// </summary>
        /// <typeparam name="T">集合元素的数据类型</typeparam>
        /// <param name="sourceList">源集合</param>
        /// <param name="list1">要过滤的集合</param>
        /// <param name="list2">要过滤的集合</param>
        public static void GetIntersectionList<T>(this List<T> sourceList, List<T> list1, List<T> list2)
        {
            if (sourceList != null && sourceList.Count > 0)
            {
                if (list1 != null && list1.Count > 0)
                {
                    foreach (T item in list1)
                    {
                        if (sourceList.Contains(item))
                        {
                            sourceList.Remove(item);
                        }
                    }
                }
                if (list2 != null && list2.Count > 0)
                {
                    foreach (T item in list2)
                    {
                        if (sourceList.Contains(item))
                        {
                            sourceList.Remove(item);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 判断指定的集合是否不为空且集合元素个数大于0
        /// </summary>
        /// <typeparam name="T">集合中元素的数据类型</typeparam>
        /// <param name="list">集合</param>
        /// <returns>返回是/否</returns>
        public static bool IsNotNull<T>(this List<T> list)
        {
            if (list != null && list.Count > 0)
            {
                return true;
            }
            return false;
        }

    }
}
